package pwd.allen;

public class SuperMissile extends Missile{

	private Tank target=null;
	private int time=3;
	public SuperMissile(int x, int y, boolean good,Tank.Direction dir, TankClient tc) {
		super(x, y, good, dir, tc);
		chooseTank();
	}
	
	private void chooseTank(){
		int min=250;
		for(Tank tank:tc.tanks) {
			int temp=Math.abs(tank.getX()-x)+Math.abs(tank.getY()-y);
			if(tank.isLive()&&temp<min) {
				min=temp;
				target=tank;
			}
		}
	}
	
	@Override
	protected void move() {
		if(time!=0) {
			time--;
			x+=xSpeed;
			y+=ySpeed;
		}
		else if(target==null||!target.isLive()) {
			x+=xSpeed;
			y+=ySpeed;
			chooseTank();
		}
		else{
			int addX=target.getX()+15-x;
			int addY=target.getY()+15-y;
			int sum=Math.abs(addX)+Math.abs(addY);
			if(sum!=0) {
				xSpeed=16*addX/sum;
				ySpeed=16*addY/sum;
			}
			x+=xSpeed;
			y+=ySpeed;
		}
			
		if (x < 5 || y < 24 || x > TankClient.GAME_WIDTH - 3
				|| y > TankClient.GAME_HEIGHT - 3) {
			live = false;
		}
	}
	
}
